/**
 * Project  : Looker
 * Package  : net.freestu.looker.impl
 * File     : SolaNewsListener.java
 * Author   : solosky < solosky772@qq.com >
 * Date     : 2009-9-4
 * Modified : 2009-9-4
 * License  : Apache License 2.0 
 */
package net.freestu.looker.impl;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.commons.configuration.Configuration;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrResponse;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.common.SolrInputDocument;

import net.freestu.looker.common.ListenerException;
import net.freestu.looker.common.NewsEntity;
import net.freestu.looker.common.NewsListener;
import net.freestu.looker.common.NewsMeta;
import net.freestu.looker.util.NewsUtils;

/**
 *  
 */
public class SolaNewsListener implements NewsListener
{
	/**
	 * solr服务器对象
	 */
	 private CommonsHttpSolrServer solrServer;
	 
	 /**
	  * solr服务器回复
	  */
	 private SolrResponse response;
	 
	 /**
	  * logger
	  */
	 private static Logger logger = Logger.getLogger(SolaNewsListener.class);

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.NewsListener#add(net.freestu.looker.common.NewsEntity)
	 */
	@Override
	public void add(NewsEntity news) throws ListenerException
	{
		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("hash", news.getHash());
		doc.addField("title", news.getTitle());
		doc.addField("category",news.getCategory().getTitle());
		doc.addField("site", news.getCategory().getSite().getTitle());
		doc.addField("url", news.getUrl());
		doc.addField("date", news.getDate().getTime()/1000);
		doc.addField("intro", NewsUtils.getIntro((news)));
		doc.addField("thumb",NewsUtils.getThumb(news));
		doc.addField("image",NewsUtils.getImageCount(news));
		doc.addField("media", news.getMediaList().size());
		
		try {
	        response = solrServer.add(doc);
	        logger.info("SolrNewsAdd:"+response);
        } catch (SolrServerException e) {
	       throw new ListenerException("solr服务器内部错误"+e.getMessage());
        } catch (IOException e) {
        	   throw new ListenerException("solr服务器IO错误"+e.getMessage());
        }
	    logger.info( "ADD:"+response.getResponse() );
	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.NewsListener#close()
	 */
	@Override
	public void close() throws ListenerException
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.NewsListener#commit()
	 */
	@Override
	public void commit() throws ListenerException
	{
		  try {
			  response = solrServer.commit();
		      logger.info("commit:"+response.getResponse());
			  response = solrServer.optimize();
			  logger.info("optimze:"+response.getResponse());
        } catch (SolrServerException e) {
	        throw new ListenerException("提交更改时solr服务器内部出错:"+e.getMessage());
        } catch (IOException e) {
        	throw new ListenerException("提交更改时IO出错:"+e.getMessage());
        }
	      

	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.NewsListener#delete(net.freestu.looker.common.NewsMeta)
	 */
	@Override
	public void delete(NewsMeta news) throws ListenerException
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.NewsListener#init(org.apache.commons.configuration.Configuration)
	 */
	@Override
	public void init(Configuration conf) throws ListenerException
	{
		try {
			String solrNewsUrl = conf.getString("solrNewsUrl");
	        solrServer = new CommonsHttpSolrServer(solrNewsUrl);
	        logger.info(solrNewsUrl);
        } catch (MalformedURLException e) {
	        throw new ListenerException(e.getMessage());
        }
        logger.info("初始化完成");
	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.NewsListener#update(net.freestu.looker.common.NewsEntity)
	 */
	@Override
	public void update(NewsEntity news) throws ListenerException
	
	{
		// TODO Auto-generated method stub

	}

}
